package cn.mugutu.finance.service.system.impl;

import cn.mugutu.finance.domain.system.Menu;
import cn.mugutu.finance.domain.system.Resource;
import cn.mugutu.finance.domain.system.Role;
import cn.mugutu.finance.dto.BaseResult;
import cn.mugutu.finance.vo.RoleAddReqVO;
import cn.mugutu.finance.service.system.MenuService;
import cn.mugutu.finance.service.system.ResourceService;
import cn.mugutu.finance.service.system.RoleService;
import cn.mugutu.finance.utils.CollectionUtil;
import cn.mugutu.finance.utils.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService{
	private static final Logger LOGGER = LoggerFactory.getLogger(BaseServiceImpl.class);

	@Autowired
	private ResourceService resourceService;
	@Autowired
	private MenuService menuService;
	
	@Override
	public BaseResult saveOrUpdateRole(RoleAddReqVO roleForm) {
		
		Set<Long> resourcesIds = roleForm.getResourcesIds();
		Set<Resource> resources = new HashSet<>();
		for (Long rsId : resourcesIds) {
			Resource resource = resourceService.get(rsId);
			resources.add(resource);
		}
		
		Set<Long> menuIds = roleForm.getMenuIds();
		Set<Menu> menus = new HashSet<>();
		for (Long menuId : menuIds) {
			Menu menu = menuService.get(menuId);
			menus.add(menu);
		}
//		menus.add(menuService.get(1L));
		
		Role role = roleForm.getId()!=null ?get(roleForm.getId()) : new Role();
		role.setName(roleForm.getName());
		role.setResources(resources);
		role.setMenus(menus);
		role.setDescription(roleForm.getDescription());
		
//		saveOrUpdate(role);
		
		if(roleForm.getId()!=null){
			update(role);
			return new BaseResult(true, "修改成功");
		}else{
			save(role);
			return new BaseResult(true, "新增成功");
		}
		
	}

	@Override
	public Pager<Resource> findResourceByRoleId(Long roleId, int page, int rows) {
		if(roleId == null){
			return new Pager<Resource>(0, 0, 0);
		}
		String hql = "select res from Role r join r.resources res where r.id = ? ";
		return resourceService.findByPage(hql, page, rows, roleId);
	}

	@Override
	public Pager<Resource> findResourceByRoleIdNegation(Long roleId, int page, int rows) {
		List<Resource> all = resourceService.findAll();
		List<Resource> exclude = (List<Resource>) resourceService.findByHql("select res from Role r join r.resources res where r.id = ? ", roleId);
		
		//如果传入的roleId不为空就返回改角色没有的资源,如果为空就返回系统全部资源
		if(roleId!=null){
			for (Resource resource : exclude) {
				if(all.contains(resource)){
					all.remove(resource);
				}
			}
		}
		
		int totalCount = all.size();
		//ArrayList 内存分页
		Pager<Resource> pager = CollectionUtil.listToPage(all,rows,page);
		return pager;
	}

	@Override
	public BaseResult moveOutResource(Long roleId, Long resourceId) {
		if(roleId!=null && resourceId!=null){
			try {
				Role role = get(roleId);
				Resource resource = resourceService.get(resourceId);
				Set<Resource> resources = role.getResources();
				resources.remove(resource);
				role.setResources(resources);
				update(role);
				return new BaseResult(true, "移除权限成功");
			} catch (Exception e) {
				LOGGER.error("移除权限失败报错");
				e.printStackTrace();
				return new BaseResult(false, "移除权限失败");
			}
		}
		return new BaseResult(false, "移除权限失败");
	}

	@Override
	public BaseResult moveInResource(Long roleId, Long resourceId) {
		if(roleId!=null && resourceId!=null){
			try {
				Role role = get(roleId);
				Resource resource = resourceService.get(resourceId);
				Set<Resource> resources = role.getResources();
				resources.add(resource);
				role.setResources(resources);
				update(role);
			} catch (Exception e) {
				LOGGER.error("添加权限失败报错");
				e.printStackTrace();
				return new BaseResult(false, "添加权限失败");
			}
		}
		return new BaseResult(false, "添加权限失败");
	}

}
